part of 'alert.dart';

///修改密码
class AlertChangePwd extends StatelessWidget {
  AlertChangePwd({Key? key}) : super(key: key);

  final tcOldPwd = TextEditingController();
  final tcNewPwd = TextEditingController();
  final tcNew2Pwd = TextEditingController();

  SaveController get saveController => Get.find<SaveController>();

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
        child: Material(
      color: Colors.transparent,
      child: Container(
        decoration: kAlertBoxDecoration,
        clipBehavior: Clip.hardEdge,
        height: 254,
        width: 412,
        padding: EdgeInsets.all(15),
        child: DefaultTextStyle(
          style: TextStyle(
            color: Colors.white,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'Please set your password',
                style: TextStyle(
                    color: Colors.white, fontSize: kAlertTitleFontSize),
              ),
              SizedBox(
                height: 44,
                child: Row(
                  children: [
                    Container(
                      padding: EdgeInsets.only(right: 8),
                      child: Text('Old password:', textAlign: TextAlign.right),
                      width: 160,
                    ),
                    Expanded(
                      child: kAlertInput(controller: tcOldPwd),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 44,
                child: Row(
                  children: [
                    Container(
                      padding: EdgeInsets.only(right: 8),
                      child: Text('New password:', textAlign: TextAlign.right),
                      width: 160,
                    ),
                    Expanded(
                      child: kAlertInput(controller: tcNewPwd),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 44,
                child: Row(
                  children: [
                    Container(
                      padding: EdgeInsets.only(right: 8),
                      child: Text('Please enter again:',
                          textAlign: TextAlign.right),
                      width: 160,
                    ),
                    Expanded(
                      child: kAlertInput(controller: tcNew2Pwd),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    TextButton(
                      onPressed: () => Get.back(),
                      child: Text(
                        'Cancel',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                    TextButton(
                      onPressed: () {
                        try {
                          validForm();
                          saveController.changePwd(tcNewPwd.text);
                          Get.back();
                        } catch (e) {
                          Fluttertoast.showToast(msg: e.toString());
                        }
                      },
                      child: Text(
                        'OK',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    ));
  }

  void validForm() async {
    if (tcOldPwd.text.isEmpty) {
      throw 'Please enter old password';
    }
    if (tcNewPwd.text.isEmpty) {
      throw 'Please enter new password';
    }
    if (tcNew2Pwd.text.isEmpty) {
      throw 'Please enter new password again';
    }
    if (tcOldPwd.text != await saveController.lastPwd()) {
      throw 'Old password not correct';
    }
    if (tcNewPwd.text != tcNew2Pwd.text) {
      throw 'Confirm new password not equal';
    }
  }
}
